<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/11/011
 * Time: 16:26
 */

namespace app\api\service;


use think\Controller;
use think\Loader;

class Xinge extends Controller {

    /**
     * 保存用户信鸽信息
     * @param $param
     * @return array
     */
    public static function save_xinge_token($param) {
        // 获取token对应的数据
        $xinge = model('xinge')->get(['token' => $param['token']]);

        if(isset($param['member_id']) && $param['member_id'] > 0) {
            // 如果有传member_id 那么已有token数据的话就更新  否则就新增
            if (!$xinge) {
                model('xinge')->create(['member_id' => $param['member_id'], 'token' => $param['token']]);
            } else {
                model('xinge')->save(['member_id' => $param['member_id']], ['token' => $param['token']]);
            }
        } else {
            // 如果没有用户ID的话   在token不存在的情况下新增token记录
            if (!$xinge) {
                model('xinge')->create(['token' => $param['token']]);
            }
        }

        return api_return(200, []);
    }

    //安卓下发多个账号
    public static function AndroidPushAccountList($accountList, $title, $content, $custom = [], $activity = '')
    {
        Loader::import('Xinge.src.XingeApp');
        $xinge_config = config('xinge');
        $push = new \XingeApp($xinge_config['android_access_id'], $xinge_config['android_secret_key']);
        $mess = new \Message();
        $mess->setExpireTime(86400);
        $mess->setTitle($title);
        $mess->setContent($content);
        $mess->setType(\Message::TYPE_NOTIFICATION);
        $action = new \ClickAction();
        if ($activity) {
            $action->setActivity($activity);
        }
        $mess->setAction($action);

        if ($custom) {
            $mess->setCustom($custom);
        }

        $ret = $push->PushAccountList(0, $accountList, $mess);
        return ($ret);
    }

    //下发单个账号
    public static function AndroidPushSingleAccount($account, $title, $content, $custom = [], $activity = '')
    {
        Loader::import('Xinge.src.XingeApp');
        $xinge_config = config('xinge');
        $push = new \XingeApp($xinge_config['android_access_id'], $xinge_config['android_secret_key']);
        $mess = new \Message();
        $mess->setExpireTime(86400);
        $mess->setTitle($title);
        $mess->setContent($content);
        $mess->setType(\Message::TYPE_NOTIFICATION);
        if ($custom) {
            $mess->setCustom($custom);
        }
        $action = new \ClickAction();
        if ($activity) {
            $action->setActivity($activity);
        }
        $mess->setAction($action);
        $ret = $push->PushSingleAccount(0, $account, $mess);
        return ($ret);
    }

    //IOS下发多个账号
    public static function IosPushAccountList($accountList, $content = '', $custom = [], $activity = '')
    {
        Loader::import('Xinge.src.XingeApp');
        $xinge_config = config('xinge');
        $push = new \XingeApp($xinge_config['ios_access_id'], $xinge_config['ios_secret_key']);
        $mess = new \MessageIOS();
        $mess->setExpireTime(86400);
        $mess->setAlert($content);
        $mess->setBadge(1);
        $mess->setSound("beep.wav");
        if ($custom) {
            $mess->setCustom($custom);
        }
        $acceptTime1 = new \TimeInterval(0, 0, 23, 59);
        $mess->addAcceptTime($acceptTime1);

        $ret = $push->PushAccountList(0, $accountList, $mess, \XingeApp::IOSENV_DEV);

        return $ret;
    }

    //下发IOS账号消息
    public static function IosPushSingleAccount($account, $content = '', $custom = [], $activity = '')
    {
        Loader::import('Xinge.src.XingeApp');
        $xinge_config = config('xinge');
        $push = new \XingeApp($xinge_config['ios_access_id'], $xinge_config['ios_secret_key']);
        $mess = new \MessageIOS();
        $mess->setExpireTime(86400);
        $mess->setAlert($content);
        //$mess->setAlert(array('key1'=>'value1'));
        $mess->setBadge(1);
        $mess->setSound("beep.wav");
        if ($custom) {
            $mess->setCustom($custom);
        }
        $acceptTime1 = new \TimeInterval(0, 0, 23, 59);
        $mess->addAcceptTime($acceptTime1);
        $ret = $push->PushSingleAccount(0, $account, $mess, \XingeApp::IOSENV_DEV);
        return $ret;
    }
}